package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.androidnetframe.utils.DateUtils;
import com.mmlc.inter.InterAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.responsebean.PrepareResponseBean;
import com.mmlc.utils.ViewUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/8/31.
 */
public class DiscountCouponAdapter extends InterAdapter<PrepareResponseBean.DataBean.CouponListBean> {
    int index = 0;

    public DiscountCouponAdapter(Context context, List<PrepareResponseBean.DataBean.CouponListBean> datas) {
        this(context, datas, 0);
    }

    public DiscountCouponAdapter(Context context, List<PrepareResponseBean.DataBean.CouponListBean> datas, int index) {
        super(context, datas);
        this.index = index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.item_discount_coupon, parent, false);

        TextView tv_name = ViewUtils.ViewHolder.get(convertView, R.id.tv_name);
        TextView tv_number = ViewUtils.ViewHolder.get(convertView, R.id.tv_number);
        TextView tv_type = ViewUtils.ViewHolder.get(convertView, R.id.tv_type);
        TextView tv_description = ViewUtils.ViewHolder.get(convertView, R.id.tv_description);
        TextView tv_time = ViewUtils.ViewHolder.get(convertView, R.id.tv_time);

        if (index == 0) {
            ViewUtils.ViewHolder.get(convertView, R.id.rootView).setBackgroundResource(R.drawable.ticket_unused);
            tv_name.setTextColor(context.getResources().getColor(R.color.text_ffdede));
        } else {
            ViewUtils.ViewHolder.get(convertView, R.id.rootView).setBackgroundResource(R.drawable.ticket_overdue);
            tv_name.setTextColor(context.getResources().getColor(R.color.text_white));
        }

        PrepareResponseBean.DataBean.CouponListBean bean = datas.get(position);

        if (!ViewUtils.isEmpty(bean.getName())) tv_name.setText(bean.getName());
        if (!ViewUtils.isEmpty(bean.getMoney())) {
            SpannableString sb = new SpannableString("¥" + Double.parseDouble(bean.getMoney()));
            sb.setSpan(new AbsoluteSizeSpan(17, true), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
            tv_number.setText(sb);
        }
        if (!ViewUtils.isEmpty(bean.getRemark())) tv_type.setText(bean.getRemark());
        if (!ViewUtils.isEmpty(bean.getMin_price()))
            tv_description.setText("满" + Double.parseDouble(bean.getMin_price()) / 1 + "元可用");
        if (!ViewUtils.isEmpty(bean.getUse_start_date()) && !ViewUtils.isEmpty(bean.getUse_end_date())) {
            String startTime = DateUtils.changeDateStyle(bean.getUse_start_date(), "yyyy-MM-dd HH:mm:ss", "yyyy.MM.dd");
            String endTime = DateUtils.changeDateStyle(bean.getUse_end_date(), "yyyy-MM-dd HH:mm:ss", "yyyy.MM.dd");
            if (!ViewUtils.isEmpty(startTime) && !ViewUtils.isEmpty(endTime))
                tv_time.setText(startTime + "-" + endTime);
        }

        return convertView;
    }
}
